home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Fortran to C conversi
- Date: 6 Mar 1996 07:21:46 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4hjeea$qgj@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- dkolmar@grovestocktn.com (Doug Kolmar) in
- <4hf04k$ogs@pipe3.nyc.pipeline.com> asks:
-
- >I'm sort of a weekend C programmer so perhaps I've missed something, but
- >I'm trying to convert a function in FORTRAN to C.
-
- >Attached is the FORTRAN code followed by the C code that I've written. The
- >C program will compile and run, but does not yield the correct output. I.E.
- >integers in the range 0 to 31.
-
- BTW, your posted Fortran code has no label `20' even though it is the target
- of `IF (K.GE.1) GOTO 20'.
-
- /* First: here is a direct translation of the function: */
-
- #include<stdlib.h> /* for srand(), rand() */
- #include<stdio.h> /* for printf() */
- #include<time.h> /* for time() */
-
- int i10vrf(int last)
- { /* FUNCTION I1OVRF(LAST) */
- int new = 0, /* NEW=0 */
- k = 16, /* K=16 */
- l = last, /* L=LAST */
- j;
- double probit = 0.03125, /* PROBIT = .03125 */
- u;
- do { /* mha - is this where the label `20'
- * goes? */
- j = l / k; /* J=L/K */
- if (j == 1)
- l -= k; /* IF (J.EQ.1) L=L-K */
- u = (double) rand() / (RAND_MAX + 1); /* U=RAND(0) */
- if (u < probit)
- j = 1 - j; /* IF (U.LT.PROBIT) J=1-J */
- new += j * k; /* NEW = NEW+J*K */
- k /= 2; /* K=K/2 */
- probit *= 2; /* PROBIT=PROBIT*2 */
- } while (k >= 1); /* IF (K.GE.1) GO TO 20 */
- return new; /* I1OVRF=NEW */
- /* RETURN */
- } /* END */
-
-
- /* and here is a driver to test it */
- int main()
- {
- int i, last, new = 30;
-
- srand((unsigned) time(NULL));
- for (i = 0; i < 20; ++i) {
- last = new;
- new = i10vrf(last);
- printf("new= %d\n", new);
- }
- return 0;
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-